const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/test', { useNewUrlParser: true, useUnifiedTopology: true });

var db = mongoose.connection;

db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function () {
  console.log('we\'re connected!');
});

//cities表
const citiesSchema = mongoose.Schema({
  name: String,
  url: String,
  aqi: Number,
  level: String,
  pos: String,
  time: Date
});

const Cities = mongoose.model('Cities', citiesSchema);

function addCities(name, url, aqi, level, pos, time) {
  let city = new Cities({ name: name, url: url, aqi: aqi, level: level, pos: pos, time: time });
  // console.log(city);
  city.save((err, data) => {
    if (err) console.log(err);
    // console.log(`data has been inserted`);
  });
}

function updateCitiesPos(query, pos) {
  Cities.updateMany(query, {$set: { pos: pos }}, (err, raw) => {
    if (err) console.log(err);
    // console.log(query.name, pos);
    // console.log(typeof pos);
    console.log('the raw response from mongo was', raw);
    
  });
}

function updateCities(query, aqi, level) {
  Cities.update(query, {$set: { aqi: aqi, level: level}}, (err, raw) => {
    if (err) console.log(err);
    // console.log(name, pos);
    // console.log(typeof pos);
    console.log('the raw response from mongo was', raw);
  });
}

// function updateCitiesPos(query, pos) {
//   console.log(pos);
//   Cities.update(query, {$set: {pos: pos}}, (err, raw) => {
//     if (err) console.log(err);
//     // console.log(name, pos);
//     // console.log(typeof pos);
//     console.log('the raw response from mongo was', raw);
//   });
// }



function getCities(query, selectedFields, next) {
  Cities.find(query,selectedFields, (err, data) => {
    if (err) handleError(err);
    // console.log(data);
    next(data);
  }).sort({time: 'desc'});
}

function getCitiesTopTen(query, selectedFields, next) {
  Cities.find(query, selectedFields, (err, data) => {
    if(err) handleError(err);
    next(data);
  }).sort({aqi: 'asc'}).limit(12);
}

function getCitiesBottomTen(query, selectedFields, next) {
  Cities.find(query, selectedFields, (err, data) => {
    if(err) handleError(err);
    next(data);
  }).sort({aqi: 'desc'}).limit(12);
}

function getCitiesHistory(query, selectedFields, next) {
  Cities.find(query, selectedFields, (err, data) => {
    if(err) handleError(err);
    next(data);
  }).sort({time: 'desc'}).limit(24);
}

// getCitiesBottomTen({time: new Date('2020-09-12T15:00:00+08:00')}, 'name aqi level time', (data) => {
//   console.log(data);
// });

// function getCities() {
//   Cities.find((err, data) => {
//     if (err) console.log(err);
//     console.log(data);
//   })
// }

const cityDetailSchema = mongoose.Schema({
  city: String,
  station: String,
  aqi: Number,
  level: String,
  pm25: Number,
  pm10: Number,
  mainPollution: String,
  pos: String,
  time: Date
});

const CityDetail = mongoose.model('CityDetail', cityDetailSchema);

function addCityDetail(city, station, aqi, level, pm25, pm10, mainPollution, pos, time) {
  let cityDetail = new CityDetail({
    city: city,
    station: station,
    aqi: aqi,
    level: level,
    pm25: pm25,
    pm10: pm10,
    mainPollution: mainPollution,
    pos: pos,
    time: time
  });

  cityDetail.save((err, data) => {
    if (err) console.log(err);
    // console.log(`data has been inserted into database`);
  });
}

function getCityDetail(query, selectedFields, next) {
  CityDetail.find(query, selectedFields, (err, data) => {
    if (err) console.log(err);
    // console.log(data);
    next(data);
  });
}

function updateCityDetail(city, station, pos) {
  CityDetail.updateMany({ city: city, station: station }, {$set: { pos: pos }}, (err, raw) => {
    if (err) console.log(err);
    // console.log(city, station, pos);
    // console.log(typeof pos);
    console.log('the raw response from mongo was', raw);
  });
}

// getCities({ name: '北京' }, 'name url aqi level pos time', (data) => {
//   console.log(data);
// });

// Cities.remove({}, (err) => {
//   if (err) console.log(err);
//   getCities();
// });

// getCities(()=>{});
// getCityDetail();
// CityDetail.remove({}, (err) => {
//   if (err) console.log(err);
//   getCityDetail();
// });

// getCities();
// getCityDetail();

//Users表
const usersSchema = mongoose.Schema({
  username: String,
  password: String
});

const Users = mongoose.model('Users', usersSchema);
// 新建用户
function addUser(data) {
  let User = new Users({
    username: data.username,
    password: data.password
  });

  User.save((err, data) => {
    if (err) console.log(err);
    console.log(`${data} has been inserted into database`);
  });
}

function getUser(data, next) {
  Users.findOne({ username: data.username, password: data.password }, (err, data) => {
    if (err) console.log(err);
    next(data);
  });
}
// 修改密码
function updateUser(data) {
  Users.updateOne({ username: data.username }, {$set: { password: password }}, (err, raw) => {
    if (err) console.log(err);
    // console.log(city, station, pos);
    // console.log(typeof pos);
    console.log('the raw response from mongo was', raw);
  });
}

function close() {
  db.close();
}

// 管理员账户
// addUser({
//   username: 'admin',
//   password: '123456'
// })

module.exports = {
  addCityDetail: addCityDetail,
  addCities: addCities,
  getCityDetail: getCityDetail,
  getCitiesBottomTen: getCitiesBottomTen,
  getCities: getCities,
  getCitiesHistory: getCitiesHistory,
  getCitiesTopTen: getCitiesTopTen,
  updateCities: updateCities,
  updateCitiesPos: updateCitiesPos,
  updateCityDetail: updateCityDetail,
  getUser: getUser,
  addUser: addUser,
  updateUser: updateUser,
  close: close
}
